<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\HealthAdvice;
use App\HealthComment;
use Illuminate\Http\Request;
use DB;
use Mockery\Exception;

/**
 * Class HealthAdviceController
 * @package App\Http\Controllers
 */
class HealthAdviceController extends Controller
{
    /**
     * HealthAdviceController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 健康咨询列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function lists(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::HealthAdviceList());
        }

        return view('health_advice.lists');
    }


    /**
     * 删除
     * @param $advice_id
     * @return array
     * @throws \Exception
     */
    public function healthAdviceDel($advice_id)
    {
        $advice = HealthAdvice::find($advice_id);
        if (!$advice || !$advice->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
    }

    /**
     * 设置热门
     * @param $advice_id
     * @return array
     */
    public function healthAdviceHot($advice_id)
    {
        $advice = HealthAdvice::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->is_hot = HealthAdvice::HOT_YES;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 取消热门
     * @param $advice_id
     * @return array
     */
    public function healthAdviceHotoff($advice_id)
    {
        $advice = HealthAdvice::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->is_hot = HealthAdvice::HOT_NO;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 添加
     * @param Request $request
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function publish(Request $request)
    {
        if ($request->isMethod('post')) {
            return HealthAdvice::Publish($request);
        }
        return view('health_advice.publish');
    }

    /**
     * 修改
     * @param $advice_id
     * @param Request $request
     * @return $this|array
     */
    public function modify($advice_id, Request $request)
    {
        $advice = HealthAdvice::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }

        if ($request->isMethod('post')) {
//            dd($request->all());
            return HealthAdvice::Modify($advice, $request);
        }

        //封面图
        $cover = DB::table('health_pic')->select('id', 'path')->where('health_id', $advice_id)->get()->toArray();
        return view('health_advice.modify')->with(compact('advice','cover'));
    }

    /**
     * 视频列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function videoList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::healthAdviceVideoList());
        }

        return view('health_advice.videoList');
    }

    /**
     * 添加视频
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function videoPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = HealthAdvice::VideoPublish($request);
            if ($return['status'] === 1) {
                return redirect('/healthAdvice/videoList')->with('status', $return['status'])->with('message', $return['message']);
            } else {
                return back()->with('status', $return['status'])->with('message', $return['message']);
            }
        }
        return view('health_advice.videoPublish');
    }


    /**
     * 健康咨询类型
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function typeList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::healthAdviceTypeList());
        }

        return view('health_advice.typeList');
    }

    /**
     * 添加健康咨询类型
     * @param Request $request
     * @return array
     */
    public function typeAdd(Request $request)
    {
        try {
            if (!DB::table('health_advice_type')->insert([
                'type' => $request->type,
            ])) {
                return ['message' => '添加失败', 'status' => 2];
            }
            return ['message' => '添加成功', 'status' => 1];
        } catch (Exception $exception) {
            return ['message' => '添加失败', 'status' => 2];
        }
    }

    /**
     * 删除健康咨询类型
     * @param $id
     * @return array
     */
    public function typeDel($id)
    {
        try {
            if (!DB::table('health_advice_type')->where('id', $id)->delete()) {
                return ['message' => '删除失败', 'status' => 2];
            }
            return ['message' => '删除成功', 'status' => 1];
        } catch (Exception $exception) {
            return ['message' => '删除失败', 'status' => 2];
        }
    }


    /**
     * @param $p_id
     * @return \Illuminate\Http\JsonResponse
     */
    public function picDel($p_id)
    {
        if (DB::table('health_pic')->where('health_id', DB::table('health_pic')->where('id', $p_id)->value('health_id'))->count() <= 1) {
            return response()->json(['message' => '至少保留一张', 'status' => 2]);
        }
        return DB::table('health_pic')->where('id', $p_id)->delete() ? response()->json(['message' => '删除成功', 'status' => 1]) : response()->json(['message' => '删除失败', 'status' => 2]);
    }

    /**
     * 回复资讯
     * @param HealthAdvice $healthAdvice
     * @param Request $request
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function repay(HealthAdvice $healthAdvice, Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::Repays($healthAdvice->id));

        }
        return view('health_advice.repay')->with(compact('healthAdvice'));
    }


    public function repays(Request $request)
    {
        if (!$request->contents) {
            return response()->json(['message' => '你的回复呢？', 'status' => 2]);
        }
        return response()->json(HealthComment::Repay($request));
    }
}
